package com.ccptmvp.activitymvp;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import butterknife.ButterKnife;
/**
 * @author dengpp
 */
public abstract class BaseActivity<P extends ActivityIPresenter> extends AppCompatActivity implements ActivityIView {
    // Presenter对象
    protected P MvpPre;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //保持竖屏
        //保持竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        MvpPre = bindPresenter();

        //子类不再需要设置布局ID，也不再需要使用ButterKnife.bind()
        setContentView(provideContentViewId());
        ButterKnife.bind(this);
        initView();
    }

    // 绑定Presenter
    protected abstract P bindPresenter();

    //得到当前界面的布局文件id(由子类实现)
    protected abstract int provideContentViewId();

    //ui及监听初始化操作
    protected abstract void initView();

    @Override
    public Activity getSelfActivity() {
        return this;
    }


    @Override
    public AppCompatActivity getAppCompatActivity() {
        return this;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        /**
         * 在生命周期结束时，将presenter与view之间的联系断开，防止出现内存泄露
         */
        if (MvpPre != null) {
            MvpPre.detachView();
        }
    }
}
